package top.z.work.module.system.controller.dept;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.z.work.framework.common.pojo.PageInfo;
import top.z.work.framework.common.pojo.R;
import top.z.work.module.system.controller.dept.vo.SystemDeptCreateVO;
import top.z.work.module.system.controller.dept.vo.SystemDeptModifyVO;
import top.z.work.module.system.controller.dept.vo.SystemDeptVO;
import top.z.work.module.system.convert.SystemDeptConvert;
import top.z.work.module.system.entity.SystemDept;
import top.z.work.module.system.service.SystemDeptService;

import java.util.List;

/**
 * 部门管理Controller
 *
 * @author zaoangod
 * @since 2024-05-10 16:38:44
 */
@Tag(name = "部门管理")
@Slf4j
@Validated
@RestController
@RequestMapping("system/dept")
public class SystemDeptController {

    @Resource
    private SystemDeptService systemDeptService;

    @Operation(summary = "部门分页", description = "获取部门分页")
    @GetMapping("page")
    public R<PageInfo<SystemDeptVO>> list(
            SystemDeptVO parameter,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "size", defaultValue = "10") Integer size
    ) {
        QueryWrapper     query    = SystemDeptConvert.INSTANCE.convert(parameter);
        Page<SystemDept> data     = Page.of(page, size);
        Page<SystemDept> pageInfo = systemDeptService.page(data, query);
        return R.s(SystemDeptConvert.INSTANCE.convert(pageInfo));
    }

    @Operation(summary = "部门列表", description = "获取部门列表")
    @GetMapping("list")
    public R<List<SystemDeptVO>> list(SystemDeptVO parameter) {
        QueryWrapper     query = SystemDeptConvert.INSTANCE.convert(parameter);
        List<SystemDept> list  = systemDeptService.list(query);
        return R.s(SystemDeptConvert.INSTANCE.convert(list));
    }

    @Operation(summary = "部门信息", description = "获取部门信息")
    @GetMapping("info")
    public R<SystemDeptVO> queryOne(SystemDeptVO parameter) {
        QueryWrapper query      = SystemDeptConvert.INSTANCE.convert(parameter);
        SystemDept   systemDept = systemDeptService.getOne(query);
        return R.s(SystemDeptConvert.INSTANCE.convert(systemDept));
    }

    @Operation(summary = "删除部门", description = "根据部门ID删除数据")
    @DeleteMapping("remove")
    public R<Boolean> remove(@RequestParam("identity") @NotNull(message = "数据主键不能为空") Long identity) {
        boolean success = systemDeptService.removeDept(identity);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "修改部门", description = "根据部门ID修改数据")
    @PutMapping("modify")
    public R<Boolean> modify(@RequestBody SystemDeptModifyVO parameter) {
        SystemDept dept    = SystemDeptConvert.INSTANCE.convert(parameter);
        boolean    success = systemDeptService.updateById(dept);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "添加部门", description = "添加部门数据")
    @PostMapping("add")
    public R<Boolean> add(@RequestBody SystemDeptCreateVO parameter) {
        SystemDept dept    = SystemDeptConvert.INSTANCE.convert(parameter);
        boolean    success = systemDeptService.save(dept);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "批量删除", description = "批量删除部门数据")
    @PostMapping("batch-remove")
    public R<Boolean> batchRemove(
            @RequestBody
            @NotNull(message = "数据主键列表不能为空")
            @NotEmpty(message = "数据主键列表不能为空")
            List<String> identityList
    ) {
        boolean success = systemDeptService.removeByIds(identityList);
        return success ? R.s(true) : R.f();
    }

}
